//
//  HMHomeOriginalView.swift
//  WeiBo
//
//  Created by Apple on 2017/5/31.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

//  原创微博
class HMHomeOriginalView: UIView {
    
    //  记录原创微博的底部约束
    var originalViewConstraint: Constraint?
    
    //  引用ViewModel，绑定数据
    var statusViewModel: HMStatusViewModel? {
        didSet {
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            //  内容
            contentLabel.text = statusViewModel?.status?.text
            
            //  先卸载之前的原创底部约束
            originalViewConstraint?.deactivate()
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //  有原创微博配图
                pictureView.isHidden = false
                self.snp.makeConstraints({ (make) in
                    originalViewConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  绑定数据
                pictureView.picUrls = picUrls
            } else {
                //  没有原创微博配图
                pictureView.isHidden = true
                self.snp.makeConstraints({ (make) in
                    originalViewConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
                
            }
            
        }
    }
    
    //  MARK:   --懒加载控件
    fileprivate lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  昵称
    fileprivate lazy var screenNameLabel: UILabel = {
        let label =  UILabel(textColor: UIColor.darkGray, fontSize: 14)
        label.text = "小太郎"
        return label
    }()
    //  时间
    fileprivate lazy var timeLabel: UILabel = {
        let label = UILabel(textColor: UIColor.orange, fontSize: 12)
        label.text = "刚刚"
        return label
    }()
    //  来源
    fileprivate lazy var sourceLabel: UILabel = {
        let label = UILabel(textColor: UIColor.lightGray, fontSize: 12)
        label.text = "来自： 新浪微博"
        return label
    }()
    //  内容
    fileprivate lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 14)
        label.text = "哈哈谁看见对方卡死的积分水电费看撒娇的福克斯京东方科技阿萨德风口浪尖阿斯蒂芬看来；家受得了；附件"
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    //  认证类型图片
    fileprivate lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  会员等级图片
    fileprivate lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //  配图
    private lazy var pictureView: HMHomePictureView = {
        let collectionView = HMHomePictureView()
        //  设置背景色
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        //  设置背景色
        backgroundColor = UIColor.white
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        userImageView.snp.makeConstraints { (make) in
            make.top.left.equalTo(self).offset(HomeCellMargin)
            //  需要设置优先级，因为userImageView的top顶部有一个约束，contentLabel顶部等于图片的底部，所以有了一个高度，需要设置优先级
            make.size.equalTo(CGSize(width: 35, height: 35)).priority(.high)
        }
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp.trailing)
            make.centerY.equalTo(userImageView.snp.bottom)
        }
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.left.equalTo(userImageView.snp.right).offset(HomeCellMargin)
        }
        mbrankImageView.snp.makeConstraints { (make) in
            make.left.equalTo(screenNameLabel.snp.right).offset(HomeCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(userImageView)
            make.left.equalTo(screenNameLabel)
        }
        sourceLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp.right).offset(HomeCellMargin)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(userImageView)
            make.top.equalTo(userImageView.snp.bottom).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //  因为配图的顶部和转发微博的底部等于匹配的底部已经确定一个高度了，在设置高度会有约束冲突，这里设置约束优先级，先设置配图高度
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
            
        }
        
        //  设置关键约束
        self.snp.makeConstraints { (make) in
            originalViewConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
    }

}
